## @package vecLib
# This file is used to provide some interfaces to SNS site, Enjoy~
#!user/bin/python
import cookielib
import urllib
import urllib2
import re
import os
import sys
import base64
import httplib

## @For renren.com
class Renren:
    ## @Constructor, login with email address and password
    # @param email (@c string) login email
    # @param password (@c string) login password
    def __init__(self,email, password):

        #build opener
        self.cookie = cookielib.CookieJar()
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))

        #do login
        print 'login...'
        loginInfo = {'email':email, 'password':password}
        urlLogin = urllib.urlencode(loginInfo)

        #get page content
        self.pageBuffer = self.opener.open('http://3g.renren.com/login.do?autoLogin=true',urlLogin).read()

        #get sender id
        sendUrlPat = re.compile('.+<form action="http://(.*)Status\.do\?(.*)" method="post">')
        m = sendUrlPat.match(self.pageBuffer)
        tempUrl = m.group(2)

        sendUrl = re.sub(r'&amp;','&',tempUrl)
        self.url = 'http://3g.renren.com/status/wUpdateStatus.do?'+sendUrl;
        print self.url

    ## @To send a status to your account
    # @param message (@c string) Message you want to send
    def sendMessage(self, message):
        messageData={'status' : message}
        poststatus = urllib.urlencode(messageData)
        f=self.opener.open(self.url,poststatus).read()
        print f

## @Class to manipulate fanfou.com
class Fanfou:

    ## @Constructor, login with email address and password
    # @param email (@c string) login email
    # @param password (@c string) login password
    def __init__(self, email, password):
        _authStr = base64.b64encode(email+':'+password)
        print "[ENCODED STRING]", self._authStr
    ## @To send a status to your account
    # @param message (@c string) Message you want to send
    def sendMessage(self, message):
        headers = {"Content-type": "application/x-www-form-urlencode",
                "Accept": "text/xml",
                "Authentication": "Basic" + self._authStr}
        params = urllib.urlencode({"status": message})
        print params
        conn = httplib.HTTPConnection("api.fanfou.com")
        conn.request("POST","/Status/update.xml",params, headers)
        response = conn.getresponse()
        print "Return code:", response.stats, " reason:", response.reason
        if response.status == 200:
            print "Message successfully sent."
        else:
            print "Send failed"




        


        
        






